package com.fzg.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fzg.common.enums.ResponseWrapper;
import com.fzg.common.tool.HttpUtils;
import com.fzg.common.tool.JSONUtilsTool;
import com.fzg.common.tool.StringUtilTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * create by fzg
 * 2022/8/16 9:49
 */

@Slf4j
@Service
public class WangYiYunService {

    // 获取网抑云音乐的网址
    @Value("${wangyiyun.baseUrl}")
    private String wangYiYunBaseUrl;

    // 获取网抑云音乐每日推荐歌单
    public ResponseWrapper getWangYiYunRecommendSongList(){
        String url = wangYiYunBaseUrl + "/personalized";
        String resultString = HttpUtils.sendGetRequest(url);
        if (resultString.equals("")){
            return ResponseWrapper.markCustomError("数据获取失败");
        }else {
            // string转JSON
            JSONObject resultJsonObject = JSON.parseObject(resultString);
            return ResponseWrapper.markSuccess(resultJsonObject);
        }
    }

    /**
     * 根据歌单id获取歌单详情
     * @param id 歌单id
     * @return ResponseWrapper
     */
    public ResponseWrapper getGeDanDetailByGeDanId(String id){
        if (id == null){
            return ResponseWrapper.markCustomError("歌单不存在！");
        }else if(id.equals("")){
            return ResponseWrapper.markCustomError("歌单不存在！");
        }else {
            String url = wangYiYunBaseUrl + "/playlist/detail";
            ArrayList<HashMap<String, String>> hashMaps = new ArrayList<>();
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("id",id);
            hashMaps.add(hashMap);

            String resultString = HttpUtils.sendGetRequest(url,hashMaps);
            if (resultString.equals("")){
                return ResponseWrapper.markCustomError("数据获取失败");
            }else {
                // string转JSON
                JSONObject resultJsonObject = JSON.parseObject(resultString);
                return ResponseWrapper.markSuccess(resultJsonObject);
            }
        }
    }

    /**
     * 根据歌单id获取歌单歌曲列表
     * @param id 歌单id
     * @return ResponseWrapper
     */
    public ResponseWrapper getGeDanSongs(String id){
        if (id == null){
            return ResponseWrapper.markCustomError("歌单不存在！");
        }else if(id.equals("")){
            return ResponseWrapper.markCustomError("歌单不存在！");
        }else {
            String url = wangYiYunBaseUrl + "/playlist/track/all";
            ArrayList<HashMap<String, String>> hashMaps = new ArrayList<>();
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("id",id);
            hashMap.put("limit",String.valueOf(30));
            hashMaps.add(hashMap);

            String resultString = HttpUtils.sendGetRequest(url,hashMaps);
            if (resultString.equals("")){
                return ResponseWrapper.markCustomError("数据获取失败");
            }else {
                // string转JSON
                JSONObject resultJsonObject = JSON.parseObject(resultString);
                return ResponseWrapper.markSuccess(resultJsonObject);
            }
        }
    }

    /**
     * 通过手机号获取验证码
     * @param phone 手机号
     * @return ResponseWrapper
     */
    public ResponseWrapper getVerificationCodeByPhone(String phone){
        if (phone == null){
            return ResponseWrapper.markCustomError("手机号为空！");
        }else if(phone.equals("")){
            return ResponseWrapper.markCustomError("手机号为空！");
        }else {
            // 如果手机号格式正确
            if (StringUtilTool.zhengzeMatchStr(phone,"^[1][3,4,5,7,8][0-9]{9}$")){
                String url = wangYiYunBaseUrl + "/captcha/sent";
                ArrayList<HashMap<String, String>> hashMaps = new ArrayList<>();
                HashMap<String, String> hashMap = new HashMap<>();
                hashMap.put("phone",phone);
                hashMaps.add(hashMap);

                String resultString = HttpUtils.sendGetRequest(url,hashMaps);
                if (resultString.equals("")){
                    return ResponseWrapper.markCustomError("获取验证码失败！");
                }else {
                    // string转JSON
                    JSONObject resultJsonObject = JSON.parseObject(resultString);
                    return ResponseWrapper.markSuccess(resultJsonObject);
                }
            }else {
                return ResponseWrapper.markCustomError("手机号格式不正确！");
            }
        }
    }


    /**
     * 根据歌曲id获取歌曲详情
     * @param id 歌曲id
     * @return ResponseWrapper
     */
    public ResponseWrapper getSongDetailById(String id){
        if (id == null){
            return ResponseWrapper.markCustomError("歌曲不存在！");
        }else if(id.equals("")){
            return ResponseWrapper.markCustomError("歌曲不存在！");
        }else {
            String url = wangYiYunBaseUrl + "/song/detail";
            ArrayList<HashMap<String, String>> hashMaps = new ArrayList<>();
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("id",id);
            hashMap.put("limit",String.valueOf(30));
            hashMaps.add(hashMap);

            JSONObject jsonObject = HttpUtils.sendGet(url, hashMaps);
            Map<String, Object> map = JSONUtilsTool.JSONObjectToMap(jsonObject);
            if (!map.get("code").equals(200)){
                return ResponseWrapper.markCustomError(map.get("msg").toString());
            }else {
                return ResponseWrapper.markSuccess(map.get("data"));
            }
        }
    }


    /**
     * 获取轮播图数据
     * @return ResponseWrapper
     */
    public ResponseWrapper getBanner(){
        String url = wangYiYunBaseUrl + "/banner";
        String resultString = HttpUtils.sendGetRequest(url);
        if (resultString.equals("")){
            return ResponseWrapper.markCustomError("数据获取失败");
        }else {
            // string转JSON
            JSONObject resultJsonObject = JSON.parseObject(resultString);
            return ResponseWrapper.markSuccess(resultJsonObject);
        }
    }


    /**
     * 验证验证码
     * @param phone 手机号
     * @param captcha 验证码
     * @return ResponseWrapper
     */
    public ResponseWrapper verifyShortMessageCode( String phone,  String captcha){
        String url = wangYiYunBaseUrl + "/captcha/verify";
        ArrayList<HashMap<String, String>> hashMaps = new ArrayList<>();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("phone",phone);
        hashMap.put("captcha",captcha);
        hashMaps.add(hashMap);
        JSONObject jsonObject = HttpUtils.sendGet(url, hashMaps);

        // jsonObject转map
        Map<String, Object> map = JSONUtilsTool.JSONObjectToMap(jsonObject);
        return ResponseWrapper.markSuccess(map);
    }

    /**
     * 根据歌曲id获取歌词
     * @param id 歌曲id
     * @return ResponseWrapper
     */
    public ResponseWrapper getSongLyric(String id){
        String url = wangYiYunBaseUrl + "/lyric";
        ArrayList<HashMap<String, String>> hashMaps = new ArrayList<>();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("id",id);
        hashMaps.add(hashMap);
        JSONObject jsonObject = HttpUtils.sendGet(url, hashMaps);
        if (jsonObject.isEmpty()){
            return ResponseWrapper.markCustomError("获取歌词失败！");
        }else {
            return ResponseWrapper.markSuccess(jsonObject);
        }
    }


    /**
     * 获取30位热门歌手
     * @param type
     * -1:全部
     * 1:男歌手
     * 2:女歌手
     * 3:乐队
     *
     *
     * @param area
     * -1:全部
     * 7华语
     * 96欧美
     * 8:日本
     * 16韩国
     * 0:其他
     *
     *
     * @return ResponseWrapper
     */
    public ResponseWrapper getThirtyHotSinger(String type,String area){
        String url = wangYiYunBaseUrl + "/artist/list";
        ArrayList<HashMap<String, String>> hashMaps = new ArrayList<>();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("type",type);
        hashMap.put("area",area);
        hashMaps.add(hashMap);
        JSONObject jsonObject = HttpUtils.sendGet(url, hashMaps);
        if (jsonObject.isEmpty()){
            return ResponseWrapper.markCustomError("获取歌手失败！");
        }else {
            return ResponseWrapper.markSuccess(jsonObject);
        }
    }


    /**
     * 获取歌手50首热门歌曲
     * @param id
     * 歌手id
     * @return ResponseWrapper
     */
    public ResponseWrapper getSingerFiftyHotSongs(String id){
        String url = wangYiYunBaseUrl + "/artist/top/song";
        ArrayList<HashMap<String, String>> hashMaps = new ArrayList<>();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("id",id);
        hashMaps.add(hashMap);
        JSONObject jsonObject = HttpUtils.sendGet(url, hashMaps);
        if (jsonObject.isEmpty()){
            return ResponseWrapper.markCustomError("获取歌手歌曲失败！");
        }else {
            return ResponseWrapper.markSuccess(jsonObject);
        }
    }


    /**
     * 检查歌曲是否可用（可播放）
     * @param id 歌曲id
     * @return ResponseWrapper
     */
    public ResponseWrapper checkSongAvailable(String id){
        log.info(id);
        String url = wangYiYunBaseUrl + "/check/music";
        ArrayList<HashMap<String, String>> hashMaps = new ArrayList<>();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("id",id);
        hashMaps.add(hashMap);
        JSONObject jsonObject = HttpUtils.sendGet(url, hashMaps);
        return ResponseWrapper.markSuccess(jsonObject);
    }

}
